#include "draglist.h"
#include <QDebug>
#include <QMimeData>
#include <QDrag>
#include <QLabel>
#include <QPixmap>
#include <QModelIndex>
#include <QScrollBar>

DragList::DragList(QWidget* parent):QListView(parent)
{
    setDragEnabled(true);
   // setVerticalScrollBar(new QScrollBar());
    //setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
}

void DragList::dragMoveEvent(QDragMoveEvent *event)
{
    QMimeData *mimeData=new QMimeData();
    mimeData->setText("TEST");
    QDragMoveEvent *e2=new QDragMoveEvent(event->pos(),Qt::CopyAction,mimeData,event->mouseButtons(),event->keyboardModifiers());
    event=e2;
    //event->mimeData()->setText("TEST_MIME_DATA");
    qDebug()<<"Start dragging...";
}

void DragList::mousePressEvent(QMouseEvent *event)
{
    qDebug()<<"Mouse pressing event...dragging";

    QString itemText=indexAt(event->pos()).data().toString();
    QLabel *label=new QLabel(itemText);  //check, if item clicked
    if(!label)                                                  //if not
        return;                                                 //return

    QPoint hotSpot=event->pos()-label->pos();
    QMimeData *mimeData=new QMimeData();
    mimeData->setText(itemText);

    QPixmap pixmap(label->sizeHint());
    label->render(&pixmap);

    QDrag *drag=new QDrag(this);

    drag->setMimeData(mimeData);
    drag->setPixmap(pixmap);
    drag->setHotSpot(hotSpot);

    Qt::DropAction dropAction=drag->exec(Qt::CopyAction|Qt::MoveAction);

    if(dropAction==Qt::MoveAction)
    {
        label->close();
        delete label;
    }

}
